package service;

import mapper.ArticleMapper;
import mapper.LoginMessageMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import po.Article;
import po.LoginMessage;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service(value = "insertServiceImpl")
public class InsertServiceImpl implements InsertService {

    //日志文件类
    private static final Logger logger = LoggerFactory.getLogger(InsertServiceImpl.class);

    //自动注入数据库接口文件的代理类
    @Autowired
    private LoginMessageMapper loginMessageMapper;
    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public boolean insertAccount(LoginMessage requestLoginMessage){

        String account = requestLoginMessage.getAccount();
        String password = requestLoginMessage.getPassword();

        logger.debug("account的注册值是：" + account);
        logger.debug("password的注册值是：" + password);
        try {
            loginMessageMapper.insertAccount(requestLoginMessage);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public boolean insertArticle(Article requestArticle){



        String title = requestArticle.getTitle();
        String author = requestArticle.getAuthor();

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String date = df.format(requestArticle.getDate());

        String details = requestArticle.getDetails();
        String note = requestArticle.getNote();

        logger.debug("题目是：" + title);
        logger.debug("作者是：" + author);
        logger.debug("日期是：" + date);
        logger.debug("内容是：" + details);
        logger.debug("备注是：" + note);
        try {
            articleMapper.insertArticle(requestArticle);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
